<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 创建相册 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除所选相册 </button>
            </div>
        </script>
        <table class="layui-hide" id="currentTableId2" lay-filter="currentTableFilter2"></table>

        <script type="text/html" id="currentTableBar2">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit2">编辑</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-upload" lay-event="upload">上传资源</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="preview">查看</a>
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table','miniPage','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;

        table.render({
            elem: '#currentTableId2',
            url: BasePath+'api/SchoolAlbum/',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {title: '操作', width: 220, toolbar: '#currentTableBar2', align: "center"},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'title', width: 120, title: '标题', sort: true},
                {field: 'description', width: 280, title: '描述'},
                {field: 'isPublic', width: 120, title: '是否公开'},
                // {field: 'singns', title: '标签', minWidth: 150},
                // {field: 'albums', title: '相册数', minWidth: 150},
                // {field: 'views', width: 135, title: '点击量', sort: true}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        //相册删除函数
        function deleteAlbum(albumId){
            var result=false;
            jq1111.ajax({
                type: "POST",
                crossDomain:true,
                async:false,
                url: BasePath+"api/SchoolAlbum/"+albumId+"/Delete",
                contentType: "application/json",
                dataType:"json",
                data:JSON.stringify({

                }),
                xhrFields: {
                    withCredentials: true
                },
                success:function (message) {
                    result=true;
                },
                error:function (message) {
                    console.log(message.responseJSON);
                    result=false;
                },
            });

            return result;
        }

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('currentTableId2', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter2)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('page/admin/school-album-manage/add.html');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '创建相册',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if(obj.event === 'delete'){ // 设置删除操作
                var checkStatus = table.checkStatus('currentTableId2')
                    , data = checkStatus.data;
                if(data.length===0){
                    layer.alert('未选择要删除的数据', {
                        title: '操作不合法'
                    });
                    return ;
                }
                var deletedItemCount=0;
                data.forEach(function (deleteData){
                    if(deleteAlbum(deleteData.id)){
                        deletedItemCount++;
                    }
                });
                //TODO 有计数bug，全部显示失败实际成功
                layer.alert('成功删除'+deletedItemCount+'个数据,'+(data.length-deletedItemCount)+'条删除失败，如果存在删除失败，请尝试刷新界面重试', {
                    title: '操作结果'
                });
                table.reload('currentTableId2',{}, 'data');
                console.log(data)
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter2)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter2)', function (obj) {
            var data = obj.data;
            //编辑相册
            if (obj.event === 'edit2') {
                var content = miniPage.getHrefContent('page/admin/school-album-manage/edit.html');
                var openWH = miniPage.getOpenWidthHeight();
                //TODO 需要一个更好的传参方法，先这样用着吧
                localStorage.setItem('selectedSchoolAlbumId',data.id);

                var index = layer.open({
                    title: '编辑相册',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'upload') {
                var content = miniPage.getHrefContent('page/student-gallery-manage/upload.html');
                var openWH = miniPage.getOpenWidthHeight();
                //TODO 需要一个更好的传参方法，先这样用着吧
                localStorage.setItem('selectedAlbumId',data.id);

                var index = layer.open({
                    title: '上传资源',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }else if(obj.event === 'preview'){ // 设置删除操作
                window.location = 'page/new-show.html?SchoolAlbumId='+data.id;
            }
        });

    });
</script>